package com.projectsexception.festivales.setup.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.projectsexception.festivales.setup.BandInfo;
import com.projectsexception.festivales.setup.Channel;
import com.projectsexception.festivales.setup.OfficialChannelInfo;
import com.projectsexception.festivales.setup.ShowInfo;
import com.projectsexception.festivales.setup.StageInfo;
import com.projectsexception.festivales.util.DateFormater;

public class ShowInfoHandler extends DefaultHandler {
    
    private List<ShowInfo> shows;
    private Map<String, BandInfo> bands;
    private Map<String, StageInfo> stages;
    private BandInfo tmpBandInfo;
    private boolean bandBiography, bandPhotoURL;
    private transient StringBuilder sb;

    @Override
    public void startDocument() {
        shows = new ArrayList<ShowInfo>();
        bandBiography = false;
        bandPhotoURL = false;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (s.equals("bands")) {
            bands = new HashMap<String, BandInfo>();
        } else if (s.equals("band")) {
            tmpBandInfo = new BandInfo();            
            tmpBandInfo.setId(Integer.parseInt(attributes.getValue("id")));
            tmpBandInfo.setName(attributes.getValue("name"));
            bands.put(attributes.getValue("id"), tmpBandInfo);
        } else if (s.equals("photoURL")) {
            bandPhotoURL = true;
        } else if (s.equals("biography")) {
            bandBiography = true;
            sb = new StringBuilder();
        } else if (s.equals("officialChannels")) {
            tmpBandInfo.setOfficialChannels(new ArrayList<OfficialChannelInfo>());
        } else if (s.equals("channel")) {
            OfficialChannelInfo channel = new OfficialChannelInfo();
            channel.setType(Channel.valueOf(Channel.class, attributes.getValue("type")));
            channel.setLink(attributes.getValue("link"));
            tmpBandInfo.getOfficialChannels().add(channel);
        } else if (s.equals("stages")) {
            stages = new HashMap<String, StageInfo>();
        } else if (s.equals("stage")) {
            StageInfo stageInfo = new StageInfo();
            stageInfo.setId(Integer.parseInt(attributes.getValue("id")));
            stageInfo.setName(attributes.getValue("name"));
            stages.put(attributes.getValue("id"), stageInfo);
        } else if (s.equals("show")) {
            ShowInfo showInfo = new ShowInfo();
            showInfo.setBand(bands.get(attributes.getValue("band")));
            showInfo.setStage(stages.get(attributes.getValue("stage")));
            showInfo.setDate(DateFormater.parseDate(attributes.getValue("date")));
            showInfo.setStartTime(attributes.getValue("startTime"));
            showInfo.setEndTime(attributes.getValue("endTime"));
            shows.add(showInfo);
        }
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (bandBiography) {
            sb.append(new String(ch, start, length));
        } else if (bandPhotoURL) {
            tmpBandInfo.setPhotoURL(new String(ch, start, length));
        }
    }
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (s.equals("biography")) {
            bandBiography = false;
            tmpBandInfo.setBiography(sb.toString().trim());
        } else if (s.equals("photoURL")) {
            bandPhotoURL = false;
        }
    }

    public List<ShowInfo> getResult() {
        return shows;
    }

}
